<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Links extends MY_Controller {

	//构造函数
	public function __construct()
	{
		parent::__construct();
		$this->load->model('dxdb_model','link','links');
	}

    public function linkmsg()
    {
    	$data['links'] = $this->link->all();
    	$this->load->view('link/link_show',$data);
    }

    //友情链接添加
    public function link_add()
    {
    	if(IS_POST)
    	{
    		$data = array();
    		$data = array(
    			'lk_title'    =>$this->input->post('lk_title'),
    			'lk_url'      =>$this->input->post('lk_url'),
    			'lk_image'    =>$this->input->post('image'),
    			'lk_sort'     =>$this->input->post('lk_sort')
    	     );
    		$flag = $this->link->dx_insert($data);
    		if($flag) $this->success('links/linkmsg');
    	}
    	else
    	{
    		$this->load->view('link/link_add');
    	}
    }

    //友情链接编辑
    public function link_edit()
    {
    	$eid = $this->uri->segment(3);
    	if(IS_POST)
    	{
    		$data = array();
			$data = array(
				'lk_title'    =>$this->input->post('lk_title'),
				'lk_url'      =>$this->input->post('lk_url'),
				'lk_image'    =>$this->input->post('image'),
				'lk_sort'     =>$this->input->post('lk_sort')
		     );
    		$flag = $this->link->dx_update($data,array('lk_id'=>$eid));
    		if($flag) $this->success('links/linkmsg');
    	}
    	else
    	{
    		$data = $this->link->one(array('lk_id'=>$eid));
    		$this->load->view('link/link_edit',$data);
    	}
    }

    //友情链接删除
    public function link_del()
    {
    	$id = intval($this->input->post('id'));
    	//删除图片
    	$flag = $this->link->dx_delete(array('lk_id'=>$id));
    	if($flag)
    	{
    	  $arr['status']  = 1; $arr['message']  = "删除信息成功 :)";
    	}
    	else
    	{
    	   $arr['status']  = 0; $arr['message']  = "操作失败 :(";         
    	} 
    	$this->ajax($arr); 
    }

    //图片上传处理
    public function sce_img_upload()
    {
    	$image = $_POST['name'];//"goods_image"
    	$image_path = '../uploads/links';//图片路径
    	$info = $this->_upload_img($image,$image_path);

    	$data = array ();
    	$data ['thumb_name'] = $image_path."/".$info['file_name'];
    	$data ['src_name'] = base_url().$image_path."/".$info['file_name'];
    	$data ['name']      = $info['file_name'];	 
    	//整理为json格式
    	$this->ajax($data);
    }
    //图片删除
    public function sce_img_del()
    {
    	$img_url = '../uploads/links/'.trim($_POST['name']);//"goods_image"
    	@unlink($img_url); 
    	echo 1;exit();
    }

    //修改状态
    public function link_status()
    {
		$id = intval($this->input->post('id'));
		$state = intval($this->input->post('state'));
		if($state == 1)
		{
		    $flag = $this->link->dx_update(array('lk_status'=>0),array('lk_id'=>$id));$msg = '操作成功：信息屏蔽!';
		}
		else
		{
		    $flag = $this->link->dx_update(array('lk_status'=>1),array('lk_id'=>$id));$msg = '操作成功：信息状态正常 :)';
		}
		if($flag) {$arr['status']  = 1;$arr['message']  = $msg;}else { $arr['status']  = 0;$arr['message']  = "操作失败 :(";} 
	    $this->ajax($arr); 
    }
}